@echo off
Cls
Set VITKExt=viToolkitExtensions

Rem Is PowerShell installed
If Exist %windir%\system32\WindowsPowerShell\v1.0\powershell.exe Goto :Present
Goto :NoPS

:Present
Rem Get PowerShell version
For /f "tokens=3" %%a IN ('reg query "HKLM\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine"\ ^|Find "PowerShellVersion"') DO (Set PSVersion=%%a)
Rem Check PowerShell version
If /i Not %PSVersion% == 2.0 Goto :Install
Rem V2, check CTP version
For /f "tokens=3" %%a IN ('reg query "HKLM\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine"\ ^|Find "CTPVersion"') DO (Set CTPVersion=%%a)
If /i Not %CTPVersion% == 10 Goto :Install

Rem Per machine or per User installation
echo Enter yes to install this module system-wide.
Set /p PerMachine="Otherwise it will be installed just for you: "
if not defined PerMachine goto :PerUser
if %PerMachine% EQU yes goto :PerMachine
:PerUser
Rem Get user module directory
Set InstMode=User
For /f "tokens=*" %%a IN ('powershell -command "(dir env:psmodulepath | %%{$_.Value}).Split(';')[0]"') DO (Set ModPath=%%a)
Goto :InstMod
:PerMachine
Rem Get machine module directory
Set InstMode=Machine
For /f "tokens=*" %%a IN ('powershell -command "(dir env:psmodulepath | %%{$_.Value}).Split(';')[1]"') DO (Set ModPath=%%a)

:InstMod
Rem Create Modules folder
If Exist "%ModPath%" Goto :ModFolderOK
MkDir "%ModPath%"
:ModFolderOK
Rem Create viToolkitExtensions folder
If Exist "%ModPath%\%VITKExt%" Goto :VITKExtFolderOK
MkDir "%ModPath%\%VITKExt%"
:VITKExtFolderOK
Rem Copy module file to folder
echo Copying the module to %ModPath%\%VITKExt%
Copy .\coreModule\%VITKExt%.psm1 "%ModPath%\%VITKExt%"

Rem Get profile information
For /f "tokens=*" %%a IN ('powershell -command "$profile.AllUsersCurrentHost"') DO (Set MachProfDir=%%a)
For /f "tokens=*" %%a IN ('powershell -command "[Environment]::GetFolderPath('MyDocuments')"') DO (Set UserProfDir=%%a)
If %InstMode% == Machine Set  ProfPath=%MachProfDir%
If %InstMode% == User Set  ProfPath=%UserProfDir%\WindowsPowerShell\profile.ps1

Rem Update profile
find /i "vitoolkitextensions" "%ProfPath%" >nul 2>&1
if %ERRORLEVEL% LSS 1 goto :AlreadyInProfile
Set /p UpdProfile="Enter yes to have the module loaded by default: "
if not defined UpdProfile goto :NoUpdProfile
if %UpdProfile% NEQ yes goto :NoUpdProfile
:UpdProfile
echo Updating profile in %ProfPath%
echo. >> "%ProfPath%"
echo # Import the VI Toolkit for Windows Community Extensions module >> "%ProfPath%"
echo Import-Module %VITKExt% >> "%ProfPath%"
echo. >> "%ProfPath%"
goto :Done

:AlreadyInProfile
echo The extensions are automatically loaded by your profile, not updating.

:NoUpdProfile
Goto :Done

:NoPS
echo You don't seem to have PowerShell installed on this machine.
Goto :Install

:PSv1
echo You have PowerShell v1 installed on this machine.
Goto :Install

:PS2CTP
echo You have PowerShell v2 installed on this machine.
If /i %CTPVersion% == 3 echo But you seem to have the CTP1 version.
If /i %CTPVersion% == 6 echo But you seem to have the CTP2 version.
Goto :Install

:Install
echo The VIToolkit Extensions require PowerShell v2 CTP3
Echo Please install PowerShell v2 CTP3.
Echo See http://www.microsoft.com/downloads/details.aspx?FamilyID=c913aeab-d7b4-4bb1-a958-ee6d7fe307bc&DisplayLang=en

:Done
